//
//  YTProgressObj.swift
//  YTImgeViewer
//
//  Created by aron on 2018/5/25.
//

import Foundation

class YTProgressObj: YTImgeLoadProgressProtocol {
    
    func containerView(_ containerObj: YTImgeContainerProtocol, containerView: UIView, setupUI: Bool) {
        if containerObj.progressView() == nil {
            let progressView = YTDefaultProgressView()
            progressView.backgroundColor = UIColor.white
            progressView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            progressView.isHidden = true
            containerObj.setProgressView(progressView: progressView)
            containerView.addSubview(progressView)
        }
    }
    
    func containerView(_ containerObj: YTImgeContainerProtocol, containerView: UIView, layoutSubviews: Bool) {
        if let progressView = self.getProgressView(containerObj: containerObj) {
            progressView.frame = CGRect(x: (containerView.frame.width - progressView.frame.width)/2, y: (containerView.frame.height - progressView.frame.height)/2, width: progressView.frame.width, height: progressView.frame.height)
        }
    }
    
    func containerView(_ containerObj: YTImgeContainerProtocol, containerView: UIView, startLoadImage: Bool) {
        self.getProgressView(containerObj: containerObj)?.isHidden = false
    }
    
    func containerView(_ containerObj: YTImgeContainerProtocol, containerView: UIView, loadingImageWithReceivedSize receivedSize: Int64, totalSize: Int64) {
        self.getProgressView(containerObj: containerObj)?.setProgress(progress: Float(receivedSize)/Float(totalSize))
    }
    
    func containerView(_ containerObj: YTImgeContainerProtocol, containerView: UIView, completeLoadImage: Bool) {
        self.getProgressView(containerObj: containerObj)?.isHidden = true
    }
    
    private func getProgressView(containerObj: YTImgeContainerProtocol) -> YTDefaultProgressView? {
        guard let progressView = containerObj.progressView() else { return nil }
        return progressView as? YTDefaultProgressView
    }
    
}
